﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IdentityModel.Policy;
using WSIMServices.model;


namespace WSIMServices
{
    public class WSIMAuthorizationPolicy : IAuthorizationPolicy
    {

        #region IAuthorizationPolicy Members

        public bool Evaluate(EvaluationContext evaluationContext, ref object state)
        {
            string username = null;
            
            if( (evaluationContext.ClaimSets.Count>0) && (evaluationContext.ClaimSets[0].Count >0))
	        {
                username = evaluationContext.ClaimSets[0][0].Resource.ToString();
	        }

            if(String.IsNullOrEmpty(username))   {
                return false;
            }

            WSIMIdentity id = new WSIMIdentity(username);
            WSIMPrincipal principal;

            List<string> roles=new List<string>();

            if (username == "guest") roles.Add("Guest");
            else roles.Add("User");

            principal = new WSIMPrincipal(id, roles);

            evaluationContext.Properties.Add("PrimaryIdentity", id);
            evaluationContext.Properties.Add("Principal", principal);

            return true;
            
            
        }

        public System.IdentityModel.Claims.ClaimSet Issuer
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IAuthorizationComponent Members

        public string Id
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
